package com.example.im.model.dao;

import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.im.model.bean.GroupInfo;
import com.example.im.model.bean.InvationInfo;
import com.example.im.model.bean.UserInfo;
import com.example.im.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class InviteTableDao {
    private DBHelper mHelper;
    public InviteTableDao(DBHelper helper) {
        mHelper = helper;
    }

    // 添加邀请
    public void addInvitation(InvationInfo invitationInfo){
        //获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行添加语句
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_REASON,invitationInfo.getReason());//原因
        values.put(InviteTable.COL_STATUS,invitationInfo.getStatus().ordinal());//状态    .ordinal()枚举enum转integer

        UserInfo user = invitationInfo.getUser();
        if (user != null){//联系人
            values.put(InviteTable.COL_USER_HXID,invitationInfo.getUser().getHxid());
            values.put(InviteTable.COL_USER_NAME,invitationInfo.getUser().getName());
        }else {//群组
            values.put(InviteTable.COL_GROUP_HXID,invitationInfo.getGroup().getGroupId());
            values.put(InviteTable.COL_GROUP_NAME,invitationInfo.getGroup().getGroupName());
            values.put(InviteTable.COL_USER_HXID,invitationInfo.getGroup().getInvatePerson());
        }
        db.replace(InviteTable.TAB_NAME,null,values);
    }

    // 获取所有邀请信息
    public List<InvationInfo> getInvitations(){
        //获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行查询语句
        String sql = "select * from "+InviteTable.TAB_NAME;
        Cursor cursor = db.rawQuery(sql,null);
        List<InvationInfo> invationInfos = new ArrayList<>();
//        String a = cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON));
        while (cursor.moveToNext()){
            InvationInfo invationInfo = new InvationInfo();
            invationInfo.setReason(cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)));//原因
            invationInfo.setStatus(int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))));

            String groupID = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));

            if (groupID == null){//联系人的邀请信息

                UserInfo userInfo = new UserInfo();
                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                invationInfo.setUser(userInfo);
            }else {//群组的邀请信息
                GroupInfo groupInfo = new GroupInfo();
                groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                invationInfo.setGroup(groupInfo);

            }
            //添加本次循环的邀请信息到总的集合中
            invationInfos.add(invationInfo);

        }
        //关闭资源
        cursor.close();
        //返回数据
        return invationInfos;
    }

// 将int类型状态转换为邀请的状态 int转枚举enum

    private InvationInfo.InvitationStatus int2InviteStatus(int intStatus){
        // 将int类型状态转换为邀请的状态


        if (intStatus == InvationInfo.InvitationStatus.NEW_INVITE.ordinal()) {

            return InvationInfo.InvitationStatus.NEW_INVITE;

        }

        if (intStatus == InvationInfo.InvitationStatus.INVITE_ACCEPT.ordinal()) {

            return InvationInfo.InvitationStatus.INVITE_ACCEPT;

        }

        if (intStatus == InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal()) {

            return InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER;

        }

        if (intStatus == InvationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()) {

            return InvationInfo.InvitationStatus.NEW_GROUP_INVITE;

        }

        if (intStatus == InvationInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal()) {

            return InvationInfo.InvitationStatus.NEW_GROUP_APPLICATION;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_INVITE_DECLINED;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_ACCEPT_INVITE;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_REJECT_APPLICATION;

        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()) {

            return InvationInfo.InvitationStatus.GROUP_REJECT_INVITE;

        }

        return null;


    }

    // 删除邀请
    public void removeInvitation(String hxId){
        if (hxId == null){
            return;
        }
        //获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行删除语句
        db.delete(InviteTable.TAB_NAME,InviteTable.COL_USER_HXID+"=?",new String[]{hxId});
    }

    // 更新邀请状态
    public void updateInvitationStatus(InvationInfo.InvitationStatus invitationStatus, String hxId){
        if (hxId == null){
            return;
        }

        //获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行更新操作
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_STATUS,invitationStatus.ordinal());
        db.update(InviteTable.TAB_NAME,values,InviteTable.COL_USER_HXID+"=?",new String[]{hxId});
    }
}
